package com.platform.service.impl;

import com.platform.dao.ExchangeCodeDao;
import com.platform.dao.GiftTypeDao;
import com.platform.entity.ExchangeCodeEntity;
import com.platform.entity.GiftTypeEntity;
import com.platform.service.ExchangeCodeService;
import com.platform.utils.R;
import com.platform.utils.StringUtils;
import com.platform.utils.excel.ImportExcelUtil;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Service("exchangeCodeService")
public class ExchangeCodeServiceImpl implements ExchangeCodeService {
    @Autowired
    private ExchangeCodeDao exchangeCodeDao;
    @Autowired
    private GiftTypeDao giftTypeDao;


    @Override
    public List<ExchangeCodeEntity> queryList(Map<String, Object> map) {
        return exchangeCodeDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return exchangeCodeDao.queryTotal(map);
    }

    @Override
    public R save(ExchangeCodeEntity exchangeCodeEntity) {
        Map<String,Object> map = new HashMap<>();
        map.put("cardNumber",exchangeCodeEntity.getCardNumber());
        map.put("exchangeCode",exchangeCodeEntity.getExchangeCode());
        int result = exchangeCodeDao.verifyRepeat(map);
        if (result>0){
            return R.error("兑换码或卡号已存在,请修改");
        }
         exchangeCodeDao.save(exchangeCodeEntity);
        return R.ok();
    }

    @Override
    public int update(ExchangeCodeEntity exchangeCodeEntity) {
        return exchangeCodeDao.update(exchangeCodeEntity);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return exchangeCodeDao.deleteBatch(ids);
    }

    @Override
    public ExchangeCodeEntity queryObject(Integer id) {
        return exchangeCodeDao.queryObject(id);
    }

    @Override
    @Transactional
    public R excelImport(List<String[]> list) {
        Map<String,Object> map = null;
        for (int i=1;i<list.size();i++){//行 0行是表头 1行才是数据
            String[] array = list.get(i);
            if(!array[0].trim().equals("")){
                map=new HashMap<>();
                map.put("giftName",array[0]);
                //根据礼包名称查询是否存在
                GiftTypeEntity  giftTypeEntity = giftTypeDao.queryGiftIsExists(map);
                if (giftTypeEntity==null){
                    return R.error("第"+i+"行的礼包名称不存在,请先新增礼包类别!");
                }
                //查询卡号兑换码是否存在
                Map<String,Object> exchangeCodeMap= new HashMap<>();
                exchangeCodeMap.put("cardNumber",array[1]);
                exchangeCodeMap.put("exchangeCode",array[2]);
                int count = exchangeCodeDao.verifyRepeat(exchangeCodeMap);
                if (count==1){
                    return R.error("第"+i+"行的兑换码卡号已存在,请修改后上传");
                }
                ExchangeCodeEntity exchangeCodeEntity = new ExchangeCodeEntity();
                exchangeCodeEntity.setGiftTypeId(giftTypeEntity.getId());
                exchangeCodeEntity.setCardNumber(array[1]);
                exchangeCodeEntity.setExchangeCode(array[2]);
                exchangeCodeDao.save(exchangeCodeEntity);
            }
        }
        return R.ok();
    }
}
